package com.yuejian_06.yuejian_06.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.bean.UserInfoBean;
import com.yuejian_06.yuejian_06.bean.UserUpdateBean;
import com.yuejian_06.yuejian_06.model.MeModel;

public class UpdateActivity extends BaseActivity {
    private TextView user_name,address,user_pwd,phonenum;
    private EditText ed_username,ed_userpwd,ed_phonenum,ed_address;
    private ImageView user_pic;
    private Button back_set,update_me;

    @Override
    void initViews() {

    }

    @Override
    void initEvents() {

    }

    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        initView();//初始化事件
        initEvent();//监听
        MeModel meModel = new MeModel();
        meModel.getUserInfo(getUser_id(),meListener);
    }

    private void initView(){
        user_name = (TextView) findViewById(R.id.user_name);
        user_pwd = (TextView) findViewById(R.id.user_pwd);
        address = (TextView) findViewById(R.id.address);
        phonenum = (TextView) findViewById(R.id.phonenum);

        ed_username = (EditText) findViewById(R.id.ed_username);
        ed_userpwd = (EditText) findViewById(R.id.ed_userpass);
        ed_phonenum = (EditText) findViewById(R.id.ed_phonenum);
        ed_address = (EditText) findViewById(R.id.ed_address);

        user_pic = (ImageView) findViewById(R.id.user_pic);
        back_set = (Button) findViewById(R.id.back_set);
        update_me = (Button) findViewById(R.id.update_me);
    }
    private void initEvent(){
        back_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        update_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String u_username = ed_username.getText().toString();
                String u_userpwd = ed_userpwd.getText().toString();
                String u_phonenum = ed_phonenum.getText().toString();
                String u_address = ed_address.getText().toString();

                MeModel meModel = new MeModel();
                meModel.UpdateUser(getUser_id(),u_username,u_userpwd,u_phonenum,u_address,meListener1);
            }
        });
    }

    private MeListener<UserInfoBean> meListener =new MeListener<UserInfoBean>() {
        @Override
        public void onResponse(UserInfoBean o) {
            UserInfoBean res=o;
            user_name.setText(res.getUser_name());
            user_pwd.setText(res.getUser_pwd());
            address.setText(res.getAddress());
            phonenum.setText(res.getPhonenum());
            if (!"".equals(res.getUser_pic()))
                Picasso.with(UpdateActivity.this).load("http://10.0.2.2/yuejian/Public/userpics/"+res.getUser_pic()).into(user_pic);
        }
        @Override
        public void onFail(String msg) {

        }
    };
    private MeListener<UserUpdateBean> meListener1 =new MeListener<UserUpdateBean>() {
        @Override
        public void onResponse(UserUpdateBean o) {
            UserUpdateBean res=o;
            String success=res.getSuccess();
            //Toast.makeText(UpdateActivity.this,success,Toast.LENGTH_LONG).show();
            if ("1".equals(success))
            {
                Toast.makeText(UpdateActivity.this,"修改成功",Toast.LENGTH_LONG).show();
                Intent intent = new Intent(UpdateActivity.this,SetpersonalActivity.class);
                startActivityForResult(intent,1);
            }
            else{
                Toast.makeText(UpdateActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(UpdateActivity.this,"修改失败ONFAIL",Toast.LENGTH_LONG).show();
        }
    };




}
